/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.murillo.chufa.entry;

import org.murillo.chufa.entry.SoftValueWeakKeyCacheEntry;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 *
 * @author Administrador
 */
public class WeakKey<K> extends WeakReference<K> {

    private WeakReference<SoftValueWeakKeyCacheEntry> parentEntry;
    private final int hash;

    public WeakKey(K referent, ReferenceQueue q, SoftValueWeakKeyCacheEntry parentEntry) {
        super(referent, q);
        this.hash = System.identityHashCode(referent);
        this.parentEntry = new WeakReference(parentEntry);
    }

    public WeakKey(K referent) {
        super(referent);
        this.hash = System.identityHashCode(referent);
        this.parentEntry = null;
    }

    public SoftValueWeakKeyCacheEntry getParentEntry() {
        return parentEntry.get();
    }

   public void setParentEntry(SoftValueWeakKeyCacheEntry parentEntry) {
      this.parentEntry = new WeakReference(parentEntry);
   }    
    
    public K getKey() {
        return this.get();
    }

    public int hashCode() {
        return hash;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o instanceof Reference) {
            K thisget = this.get();
            return thisget != null && (thisget == ((Reference) o).get());
        }
        return false;
    }

}
